package org.orbis.spikezolt;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.HorizontalAlign;

import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;

public class Tutorial extends Updater {
	public Sprite spr; // Pedestal
	public Sprite qSpr; // Question Ball
	private Sprite cloudSpr; // Cloud Image
	private Sprite cloudSprAlpha; // Cloud Image (Alpha)

	private Text lT, tT, rT, bT, cT;

	private float xx, yy = 0.0f;

	private int cloudX, cloudY = 0;
	public float alpha = 0.0f;

	private Random rand;

	public boolean active = false;
	public boolean attached = false;

	SpikeZoltActivity sa;

	Tutorial(SpikeZoltActivity sa, int x, int y, Object message) {
		this.sa = sa;
		rand = new Random();

		// dir+=4; dy = lengthdir_y(10, dir);
		spr = new Sprite(x, y - 3, sa.texManager.TR_pedestal);
		qSpr = new Sprite(x, y - 24, sa.texManager.TR_qball);
		cloudSpr = new Sprite(cloudX, cloudY, sa.texManager.TR_cloud);
		cloudSprAlpha = new Sprite(cloudX, cloudY, sa.texManager.TR_cloud);
		sa.scene.attachChild(spr);
		sa.scene.attachChild(qSpr);
		spr.setZIndex(sa.OTHER_DEPTH);
		qSpr.setZIndex(sa.OTHER_DEPTH);
		sa.scene.sortChildren();
		cloudSpr.setBlendFunction(GL10.GL_SRC_ALPHA,
				GL10.GL_ONE_MINUS_SRC_ALPHA);
		cloudSprAlpha
				.setBlendFunction(GL10.GL_ADD, GL10.GL_ONE_MINUS_SRC_ALPHA);
		cloudSpr.setAlpha(alpha);
		cloudSprAlpha.setAlpha(alpha * 0.5f);

		String mes = message.toString();
		mes = mes.substring(9);
		mes = mes.substring(0, mes.length() - 1);
		mes = mes.replaceAll("#", "\n");

		xx = 33;
		yy = 18;

		lT = new Text(xx - 1, yy, sa.texManager.bFont, mes,
				HorizontalAlign.CENTER);
		lT.setBlendFunction(GL10.GL_ADD, GL10.GL_ONE_MINUS_SRC_ALPHA);
		lT.setAlpha(alpha);

		rT = new Text(xx + 1, yy, sa.texManager.bFont, mes,
				HorizontalAlign.CENTER);
		rT.setBlendFunction(GL10.GL_ADD, GL10.GL_ONE_MINUS_SRC_ALPHA);
		rT.setAlpha(alpha);

		tT = new Text(xx, yy - 1, sa.texManager.bFont, mes,
				HorizontalAlign.CENTER);
		tT.setBlendFunction(GL10.GL_ADD, GL10.GL_ONE_MINUS_SRC_ALPHA);
		tT.setAlpha(alpha);
		bT = new Text(xx, yy + 1, sa.texManager.bFont, mes,
				HorizontalAlign.CENTER);
		bT.setBlendFunction(GL10.GL_ADD, GL10.GL_ONE_MINUS_SRC_ALPHA);
		bT.setAlpha(alpha);

		cT = new Text(xx, yy, sa.texManager.wFont, mes, HorizontalAlign.CENTER);
		cT.setBlendFunction(GL10.GL_ADD, GL10.GL_ONE_MINUS_SRC_ALPHA);
		cT.setAlpha(alpha);
	}

	public void updateAlpha() {
		if (active == true) {
			alpha += .03f;
			if (alpha >= 1.0f) {
				alpha = 1.0f;
			}
			if (attached == false) {
				attached = true;
				attachChildren();
			}

		} else if (active == false) {
			alpha -= .03f;
			if (alpha <= 0.0f) {
				alpha = 0.0f;
				if (attached) {
					detachChildren();
					attached = false;
				}
			}
		}

		lT.setAlpha(alpha);
		rT.setAlpha(alpha);
		tT.setAlpha(alpha);
		bT.setAlpha(alpha);
		cT.setAlpha(alpha);

		cloudSpr.setAlpha(alpha);
		cloudSprAlpha.setAlpha(alpha * 0.2f);
	}

	public void attachChildren() {
		sa.mainHud.attachChild(cloudSpr);
		sa.mainHud.attachChild(cloudSprAlpha);
		sa.mainHud.attachChild(lT);
		sa.mainHud.attachChild(rT);
		sa.mainHud.attachChild(tT);
		sa.mainHud.attachChild(bT);
		sa.mainHud.attachChild(cT);
	}

	public void detachChildren() {
		sa.mainHud.detachChild(cloudSpr);
		sa.mainHud.detachChild(cloudSprAlpha);
		sa.mainHud.detachChild(lT);
		sa.mainHud.detachChild(rT);
		sa.mainHud.detachChild(tT);
		sa.mainHud.detachChild(bT);
		sa.mainHud.detachChild(cT);
	}

	@Override
	public void step() {
		if (spr.collidesWith(sa.pl.getSprite())) {
			if (!active) {
				active = true;
			}
			updateAlpha();
		} else {
			if (active) {
				active = false;
			}
			if (alpha > 0.0f)
				updateAlpha();
		}

		if (active) {

			cloudSprAlpha.setPosition(
					cloudX + rand.nextInt(3) - rand.nextInt(3),
					cloudY + rand.nextInt(3) - rand.nextInt(3));

		}

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		if(attached){
			cloudSpr.detachSelf();
			cloudSprAlpha.detachSelf();
			lT.detachSelf();
			rT.detachSelf();
			tT.detachSelf();
			bT.detachSelf();
			cT.detachSelf();
			/*sa.mainHud.detachChild(cloudSpr);
			sa.mainHud.detachChild(cloudSprAlpha);
			sa.mainHud.detachChild(lT);
			sa.mainHud.detachChild(rT);
			sa.mainHud.detachChild(tT);
			sa.mainHud.detachChild(bT);
			sa.mainHud.detachChild(cT);*/
			attached = false;
		}
		spr.detachSelf();
		qSpr.detachSelf();
		//sa.scene.detachChild(spr);
		//sa.scene.detachChild(qSpr);
		
	}
}
